package com.enjoy.xbase.tools;


/**
 * 提交锁
 */
public class CommitLock {

    private boolean commitLock;//是否在提交
    private long lockTime;
    private long invalidTime = 0;

    public CommitLock() {

    }

    public CommitLock(long invalidTime) {
        this.invalidTime = invalidTime;
    }

    /**
     * 获取提交锁
     *
     * @return
     */
    public synchronized boolean getCommitLock() {
        if (commitLock) {
            if (invalidTime == 0) {
                return false;
            } else if (System.currentTimeMillis() > lockTime + invalidTime) {//锁过期
                lockTime = System.currentTimeMillis();
                commitLock = true;
                return true;
            } else {
                return false;
            }
        } else {
            lockTime = System.currentTimeMillis();
            commitLock = true;
            return true;
        }
    }

    public synchronized void setCommitUnLock() {
        commitLock = false;
    }
}
